#ifndef QOPENHARMONYPLAYERCONTROL_H
#define QOPENHARMONYPLAYERCONTROL_H

#include "qmediacontent.h"
#include "qmediaplayercontrol.h"

#include <QSize>
#include <QtCore/qcoreevent.h>

QT_BEGIN_NAMESPACE
class QOpenHarmonyWindowControl;
class QOpenHarmonyPlayer;

class QOpenHarmonyPlayerControl : public QMediaPlayerControl
{
    Q_OBJECT
public:
    QOpenHarmonyPlayerControl(QObject *parent = nullptr);
    ~QOpenHarmonyPlayerControl() override;

    void setVideoOutput(QOpenHarmonyWindowControl *control);

    QMediaPlayer::State state() const override;

    QMediaPlayer::MediaStatus mediaStatus() const override;

    qint64 duration() const override;

    qint64 position() const override;
    void setPosition(qint64 position) override;

    int volume() const override;
    void setVolume(int volume) override;

    bool isMuted() const override;
    void setMuted(bool muted) override;

    int bufferStatus() const override;

    bool isAudioAvailable() const override;
    bool isVideoAvailable() const override;

    bool isSeekable() const override;

    QMediaTimeRange availablePlaybackRanges() const override;

    qreal playbackRate() const override;
    void setPlaybackRate(qreal rate) override;

    QMediaContent media() const override;
    const QIODevice *mediaStream() const override;
    void setMedia(const QMediaContent &media, QIODevice *stream) override;

    void play() override;
    void pause() override;
    void stop() override;

    void setDisplay();

    QSize videoSize() const;

private slots:
    void setSeekable(bool seekable);
    void setAudioAvailable(bool available);
    void setVideoAvailable(bool available);
    void onWindowCreated();
    void onStateChanged(qint32);
    void onVideoSizeChanged(qint32, qint32 height);
    void setMediaStatus(QMediaPlayer::MediaStatus status);
    void onBufferingChanged(qint32 percent);
    void onError(int code, const QString &errorString);
private:
    void updateAvailablePlaybackRanges();
    void updateBufferStatus();
    void playOrPause(QMediaPlayer::State state);
    QOpenHarmonyWindowControl *m_control = nullptr;
    QOpenHarmonyPlayer *m_player = nullptr;
    QMediaPlayer::MediaStatus m_status = QMediaPlayer::NoMedia;
    QMediaPlayer::Error m_error = QMediaPlayer::NoError;
    QMediaTimeRange m_availablePlaybackRange;
    QIODevice *m_stream = nullptr;
    int m_volume = 100;
    bool m_muted = false;
    QMediaContent m_media;
    QString m_errorString;
    QSize m_videoSize;
    bool m_seekable = true;
    bool m_audioAvailable = false;
    bool m_videoAvailable = false;
    bool m_buffering = false;
    bool m_bufferPercent = 0;
    bool m_bufferFilled = false;
};

QT_END_NAMESPACE

#endif
